package com.liming.purchaseschema.webapp.wicket.page;
import com.liming.purchaseschema.api.biz.IPurchaseSchemaService;
import com.liming.purchaseschema.core.model.SchemaDefine;
import org.apache.aries.blueprint.annotation.Reference;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.RepeatingView;

import javax.inject.Inject;
import javax.inject.Named;
import java.util.List;

/**
 * Created by Administrator on 14-4-16.
 */
public class TreePanel extends Panel {
    @Named
    @Inject
    @Reference(id = "purchaseSchemaService", serviceInterface = IPurchaseSchemaService.class)
    private IPurchaseSchemaService purchaseSchemaService;

    RepeatingView treeContent;
    public TreePanel(String id ){
        super(id);
        treeContent = new RepeatingView("treeContent");
        add(treeContent);
    }

    public void createTree(MyFormPanel formPanel1){
        treeContent.removeAll();
        List objects = purchaseSchemaService.findPurchaseSchema();
        for(int i = 0 ; i < objects.size() ; i ++)
        {
            final SchemaDefine schemaDefine = (SchemaDefine)objects.get(i);
            List schemaDefines =purchaseSchemaService.findPurchaseSchemaByParent(schemaDefine);
            if(schemaDefines.size() > 0){
                treeContent.add(new TreeHaveChildNode(treeContent.newChildId(),schemaDefine,schemaDefines,formPanel1,this));
            }else{
                treeContent.add(new TreeNoChildNode(treeContent.newChildId(),schemaDefine,formPanel1,this));
            }
        }
    }
    public void removeTreeNode(SchemaDefine SchemaDefine){
        purchaseSchemaService.deleteSchemaDefine(SchemaDefine);
    }
}